<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\model\UpFile;
use app\admin\builder\Builder;
use app\BaseController;
use think\facade\Db;
use think\Request;

class Enclosure extends BaseController {
    public function show_list(Request $request){
        return Builder::table()
            ->setAction('enclosure/page?model_id=6')
            ->addToolbarDemo('alldel','全部删除',[],'enclosure/del')
            ->addToolbarDemo('add','添加',[],'enclosure/show_add')
            ->addToolbarDemo('button','清空不用的文件',[],'api/shanchuduoyutupian')
            ->setMonitorAction('switch','enclosure/change')
            ->setMonitorAction('edit','enclosure/change')
            ->setRightHeader()
            ->setColumn('input','yuan_name','原名称')
            ->setColumn('input','new_name','现名称')
            ->setColumn('input','path','路径')
            ->setColumn('img','paths','图片')
            ->setColumn('video','video','视频')
            ->setColumn('input','type','类型')
            ->setColumn('switch','status','状态','enclosure/change')
            ->setButton('edit','修改','enclosure/show_edit')
            ->setButton('del','删除','enclosure/del')
            ->setPage()
            ->fetch();
    }
    public function page(Request $request){
        $limit = $request->param('limit',10);
        $data = Db::name(getTable('enclosure'))
            ->order('id desc')
            ->paginate($limit)
            ->toArray();
        $array = ['png','jpg','jpeg','gif'];
        $video = [
            'avi',
            'mp4',
//            'wmv',
//            'mpg',
//            'mov',
            'flv',
            'Ogg',
            'WebM ',
        ];
        foreach ($data['data'] as $key=>$value){
            $endr = explode('.',$value['new_name']);
            if(in_array($endr[count($endr)-1],$array)){
                $value['paths'] = $value['path'];
            }else{
                $value['paths'] = '';
            }

            if(in_array($endr[count($endr)-1],$video)){
                $value['video'] = $value['path'];
            }else{
                $value['video'] = '';
            }
            $data['data'][$key] = $value;
        }
        return json_page_response($data);
    }
    public function change(Request $request){
        $id = $request->param('id');
        $data = Db::name(getTable('enclosure'))
            ->where('id',$id)
            ->find();
        if($data['status']==1){
            $data['status'] = 0;
        }else{
            $data['status'] = 1;
        }
        if(Db::name(getTable('enclosure'))->where('id',$id)->update($data)){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
    public function show_add(Request $request){
        return Builder::form()
            ->setAction('enclosure/add')
            ->setProject('file','file','文件','')
            ->setButton('submit','提交')
            ->setButton('back','返回')
            ->fetch();
    }
    public function show_edit(Request $request){
        $id = $request->param('id');
        $data = Db::name(getTable('enclosure'))
            ->where('id',$id)
            ->find();
        return Builder::form()
            ->setAction('enclosure/edit')
            ->setProject('none','id','',$id)
            ->setProject('input','yuan_name','原名',$data['yuan_name'])
            ->setProject('input','new_name','现名',$data['new_name'])
            ->setProject('input','path','路径',$data['path'])
            ->setProject('switch','status','状态',$data['status'])
            ->setButton('submit','提交')
            ->setButton('back','返回')
            ->fetch();
    }
    public function edit(Request $request){
        $data = $request->param();
        if($data['status']??null){
            $newdata['status'] = 1;
        }else{
            $newdata['status'] = 0;
        }
        $newdata = [];
        if($data['yuan_name']??null){
            $newdata['yuan_name'] = $data['yuan_name'];
        }
        if($data['new_name']??null){
            $newdata['new_name'] = $data['new_name'];
        }
        if($data['path']??null){
            $newdata['path'] = $data['path'];
        }

        if(Db::name(getTable('enclosure'))->where('id',$data['id'])->update($newdata)){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
    public function add(Request $request){
        return $this->success('添加成功');
    }
    public function del(Request $request){
        $id = $request->param('id');
        if(UpFile::delete($id,true)){
            return $this->success('删除成功');
        }else{
            return $this->success('删除失败');
        }

    }
    public function delteall(){
        return json_to_respos('清空成功',200);
//        $obj = new Api();
//        return $obj->shanchuduoyutupian();
    }
}